/*
* @author : TF-BJ-C064
* @creation : 2014-7-31 下午1:57:12
* @description : 
*
*/

package com.tool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class FileController {
	
	private Logger log = Logger.getLogger(FileController.class);

	// file exts
	public static String IMAGE_EXTS = ".jpg;*.bmp;*.png;*.jpeg;*.gif;*.ico";
	public static String FILE_EXTS = "*.zip;*.7z;*.tar.gz;*.tar;*.gz;";
	public static String DOC_EXTS = "*.doc;*.docx;*.ppt;*.pptx;*.xls;*.xls;";
	public static String APP_EXTS = "*.apk;*.a;";
	public static String LIB_EXTS = "*.so;*.a;*.lib;*.dll";
	public static String Allow_Exts = IMAGE_EXTS + FILE_EXTS + DOC_EXTS + APP_EXTS + LIB_EXTS ;
	
	public static String DENY_EXTS = "*.exe;*.bat;*.cmd;*.sh;*.jsp;*.jspx;*.html;*.php;*.asp;*.aspx;";
	public static String DENY_ALL = "*.*";
	
	public enum FileUploadMode { NONE, ALLOW, DENY };//过滤模式
	
	//private
	private String allowFiles =  Allow_Exts;//允许上传的文件类型
	private String denyFiles = DENY_EXTS;//拒绝上传的文件类型
	private FileUploadMode mode;//过滤模式
	private static Long MAX_FILE_SIZE = (1024 * 1024 * 1024 * 4L); // MAX SIZE IS 4GB
	private Long maxAllowSize = MAX_FILE_SIZE;//文件允许的最大值，单位字节
	private String msg = "请上传符合规范的文件";//提示信息
	
	private static FileController defaultFileController = new FileController(); 
	
	public static void main(String [] argv) throws Exception{
		
		String filename = "2014.jsp";
		System.out.println( FileController.checkAllowFile(filename, 1L));
	}
	
	public FileController(){}
	
	public FileController(String allowFiles, FileUploadMode modeIN, Long maxFileSize){
		this.setAllowFiles( allowFiles );
		this.setMode( modeIN );
		this.setMaxAllowSize( maxFileSize );
	}
	
	public static FileController getDefaultFileController(){
		if(defaultFileController==null)
			defaultFileController = new FileController();
		return defaultFileController;
	}
	
	/**
	 * 判断上传的文件是否可以上传
	 * <p>Title: isAllowFile</p>
	 * <p>Description: 
	 * 		
	 * </p>
	 * @param filename  文件名称
	 * @return
	 * 		true  : 允许上传
	 * 		false : 不允许上传
	 * @throws Exception
	 */
	public static boolean checkAllowFile(String filename, Long size ) throws Exception{
		return defaultFileController.isAllowFile(filename, size);
	}
	
	/**
	 * 判断上传的文件是否可以上传
	 * <p>Title: isAllowFile</p>
	 * <p>Description: 
	 * 		
	 * </p>
	 * @param filename  文件名称
	 * @return
	 * 		true  : 允许上传
	 * 		false : 不允许上传
	 * @throws Exception
	 */
	public boolean isAllowFile(String filename, Long size ) throws Exception {

		if(filename==null)
			throw new Exception("filename is null");
		
		//check ext name
		int index = filename.lastIndexOf(".");
		if(index<1)
			return false;
		String fileExtName = filename.substring(index, filename.length());
		fileExtName = fileExtName.toLowerCase();
		
//		log.info("ext="+fileExtName);
		
		boolean bok = false;
		if(this.mode == FileUploadMode.ALLOW ){
			bok = ( allowFiles!=null && allowFiles.toLowerCase().indexOf(fileExtName+";")>=0)? true : false;
		}else if(this.mode == FileUploadMode.DENY){
			bok = ( denyFiles==null || denyFiles.toLowerCase().indexOf(fileExtName+";")>=0)? false : true;
		}else{
			bok = ( denyFiles==null || denyFiles.toLowerCase().indexOf(fileExtName+";")>=0)? false : true;
		}
		
		//check file size 
		if( bok &&  size > this.getMaxAllowSize()){
			this.setMsg("文件不得大于500KB");
			return false;
		}
		
		return bok;
	}

	
	// getter and setter
	
	public String getAllowFiles() {
		return allowFiles;
	}

	public void setAllowFiles(String allowFiles) {
		this.allowFiles = allowFiles;
	}

	public String getDenyFiles() {
		return denyFiles;
	}

	public void setDenyFiles(String denyFiles) {
		this.denyFiles = denyFiles;
	}

	public FileUploadMode getMode() {
		return mode;
	}

	public void setMode(FileUploadMode mode1) {
		this.mode = mode1;
	}

	public Long getMaxAllowSize() {
		return maxAllowSize;
	}

	public void setMaxAllowSize(Long maxAllowSize) {
		this.maxAllowSize = maxAllowSize;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
}
